package com.community.web.master.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.model.entity.Area;
import com.community.model.entity.Charge;
import com.community.model.entity.Notice;
import com.community.web.master.domain.dto.NoticeDto;
import com.community.web.master.domain.dto.PageNoticeDto;
import com.community.web.master.domain.vo.ChargeWithUserVo;
import com.community.web.master.domain.vo.PageResult;
import com.community.web.master.exception.DataException;
import com.community.web.master.mapper.NoticeMapper;
import com.community.web.master.service.INoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李晶
 * @since 2024-07-02
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;
    /**
     *
     * @param pageNoticeDto
     * @return
     */
    @Override
    public PageResult pageQuery(PageNoticeDto pageNoticeDto) {
        //创建一个Page,泛型填对应entity
        IPage<Notice> page = new Page<Notice>(pageNoticeDto.getPage(),pageNoticeDto.getPageSize());
        //创建查询条件,更推荐使用LambdaQueryWrapper
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<Notice>();
        //条件
        if(pageNoticeDto.getNoticeStatus() !=null){
            queryWrapper.eq("notice_status",pageNoticeDto.getNoticeStatus());
        }
        noticeMapper.selectPage(page,queryWrapper);
        return new PageResult(page.getTotal(),page.getRecords());
    }

    /**
     * 根据id更新notice的状态
     * @param id
     */
    @Override
    public void updateStatus(Integer id) {
        Notice notice = noticeMapper.selectById(id);
        if(notice==null) throw new DataException("通知不存在");
        notice.setNoticeStatus(notice.getNoticeStatus()==1? 0:1);
        noticeMapper.updateById(notice);
    }

    /**
     * 新增公告
     * @param noticeDto
     */
    @Override
    public void add(NoticeDto noticeDto) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeDto,notice);
        noticeMapper.insert(notice);
    }
}
